package com.chinda.common.security;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chinda.modules.sys.entity.SysUserEntity;
import com.chinda.modules.sys.service.SysUserService;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

/**
 * @author Wang Chinda
 * @date 2020/7/11
 * @see
 * @since 1.0
 */
@Component
@AllArgsConstructor
public class SysUserDetailsService implements UserDetailsService {

    private final SysUserService sysUserService;

    /**
     * 查询用户信息
     * @param username
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public SysUserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 查询用户信息
        SysUserEntity sysUserEntity =sysUserService.getOne(new QueryWrapper<SysUserEntity>().eq("username", username));
        if (sysUserEntity!=null){
            // 组装参数
            SysUserDetails userDetails = new SysUserDetails();
            BeanUtils.copyProperties(sysUserEntity,userDetails);
            return userDetails;
        }
        return null;
    }
}
